import { defineStore } from 'pinia';
import { UserInfoRes, UserPhoneInfoRes } from '@/types/user';

interface State {
  token: string | null;
  userInfo: UserInfoRes | null;
  userPhoneInfo: UserPhoneInfoRes | null;
  pages: string[];
}

export const useUserStore = defineStore('userStore', {
  state: () => {
    return {
      token: null,
      userInfo: null,
      userPhoneInfo: null
    } as State;
  },

  getters: {
    isLogin(state) {
      return !!state.token;
    }
  },

  actions: {
    setToken(token: string, needCache = true) {
      this.token = token;
      needCache &&
        uni.setStorage({
          key: 'token',
          data: this.token
        });
    },
    getToken() {
      return this.token;
    },
    setUserInfo(userInfo: UserInfoRes, needCache = true) {
      this.userInfo = userInfo;
      needCache &&
        uni.setStorage({
          key: 'userInfo',
          data: this.userInfo
        });
    },
    getUserInfo() {
      return this.userInfo;
    }
  }
});
